package com.chat;

import com.chat.mapper.UserInfoMapper;
import com.chat.pojo.UserInfo;
import com.chat.util.DbUtil;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditProfileDialog extends JDialog {
    private JTextField nameField;
    private JTextField signField;
    private JButton saveButton;
    private JButton cancelButton;
    private UserInfo userInfo;
    private ChatFrame chatFrame;

    public EditProfileDialog(ChatFrame chatFrame, UserInfo userInfo) {
        this.chatFrame = chatFrame;
        this.userInfo = userInfo;
        setTitle("编辑个人信息");
        setSize(400, 250);
        setLocationRelativeTo(chatFrame);
        setResizable(false);
        setModal(true);

        // 主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
        mainPanel.setBackground(new Color(245, 245, 245));
        add(mainPanel);

        // 表单面板 - 使用GridBagLayout
        JPanel formPanel = new JPanel(new GridBagLayout());
        // 减小表单面板的内边距
        formPanel.setBorder(new EmptyBorder(10, 15, 15, 15));
        formPanel.setBackground(new Color(245, 245, 245));

        GridBagConstraints gbc = new GridBagConstraints();
        // 减小组件之间的间距
        gbc.insets = new Insets(6, 8, 6, 8);
        gbc.anchor = GridBagConstraints.WEST;     // 左对齐

        // 标签样式
        Font labelFont = new Font("微软雅黑", Font.BOLD, 14);
        JLabel nameLabel = new JLabel("昵称:");
        nameLabel.setFont(labelFont);
        nameLabel.setForeground(new Color(80, 80, 80));

        JLabel signLabel = new JLabel("个性标签:");
        signLabel.setFont(labelFont);
        signLabel.setForeground(new Color(80, 80, 80));

        // 输入框样式
        Font inputFont = new Font("微软雅黑", Font.PLAIN, 14);
        nameField = new JTextField(userInfo.getUsername(), 20);
        nameField.setFont(inputFont);
        nameField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                BorderFactory.createEmptyBorder(6, 8, 6, 8)
        ));

        signField = new JTextField(userInfo.getPersonSign(), 20);
        signField.setFont(inputFont);
        signField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                BorderFactory.createEmptyBorder(6, 8, 6, 8)
        ));

        // 第一行 - 昵称
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.3;  // 标签占30%宽度
        formPanel.add(nameLabel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.7;  // 输入框占70%宽度
        gbc.fill = GridBagConstraints.HORIZONTAL;
        formPanel.add(nameField, gbc);

        // 第二行 - 个性标签
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.3;
        formPanel.add(signLabel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.7;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        formPanel.add(signField, gbc);

        // 添加表单面板到主面板
        mainPanel.add(formPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
        buttonPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
        buttonPanel.setBackground(new Color(245, 245, 245));

        // 按钮样式
        Font buttonFont = new Font("微软雅黑", Font.BOLD, 14);
        saveButton = new JButton("保存");
        saveButton.setFont(buttonFont);
        saveButton.setBackground(new Color(70, 130, 180));
        saveButton.setForeground(Color.WHITE);
        saveButton.setFocusPainted(false);
        saveButton.setBorder(BorderFactory.createEmptyBorder(8, 25, 8, 25));

        cancelButton = new JButton("取消");
        cancelButton.setFont(buttonFont);
        cancelButton.setBackground(new Color(220, 220, 220));
        cancelButton.setForeground(new Color(80, 80, 80));
        cancelButton.setFocusPainted(false);
        cancelButton.setBorder(BorderFactory.createEmptyBorder(8, 25, 8, 25));

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 保存按钮事件
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newName = nameField.getText();
                String newSign = signField.getText();
                updateProfile(newName, newSign);
                dispose();
            }
        });
        // 取消按钮事件
        cancelButton.addActionListener(e -> dispose());
    }

    private void updateProfile(String newName, String newSign) {
        System.out.println("更新个人信息：" + newName + ", " + newSign);
        try (SqlSession session = DbUtil.getSession()) {
            UserInfoMapper userInfoMapper = session.getMapper(UserInfoMapper.class);

            // 更新数据库中的用户信息
            System.out.println("开始更改数据库信息");
            // userInfo.setUsername(newName);
            userInfo.setAccountname(newName);
            userInfo.setPersonSign(newSign);
            userInfoMapper.updateUserInfo(userInfo);
            System.out.println("数据库信息更改成功，开始提交数据库");
            session.commit();
            System.out.println("数据库信息提交成功");

            // 更新 ChatFrame 中的个人信息展示
            chatFrame.updateUserInfoDisplay(newName, newSign);
            System.out.println("更新 ChatFrame 中的个人信息展示成功");

            JOptionPane.showMessageDialog(this, "个人信息更新成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "更新个人信息时发生错误：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }
}